<?php
	
/**
 * 	Cloud3 PHP Framework 
 * 
 * 	This program is free software: you can redistribute it and/or modify
 * 	it under the terms of the GNU General Public License as published by
 * 	the Free Software Foundation, either version 3 of the License, or
 * 	( MUST INCLUDING DEVELOPMENT PROJECT ! ) any later version
 * 
 * 	This program is distributed in the hope that it will be useful,
 * 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * 	GNU General Public License for more details.
 * 
 * 	You should have received a copy of the GNU General Public License
 * 	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	README 
 * 
 * 	This project web framework application bundling library from source code
 * 	o	Doctrine	ORM - COMMON - DBAL. 
 * 	o	Symfony 	Yaml Component. 
 * 	o	PHPUnit		GUI - Framework.
 * 	
 * 	And Cloud3 PHP Framework only running for versioning PHP 5.3.0 , if you're
 * 	system no latest versioning PHP this project Cloud3 PHP Framework not running
 *  PLEASE UPGRADE version PHP , or lets common see <http://www.php.net/downloads/>
 * 
 * 	@category	Cloud3 Distribution
 * 	@copyright	Shinobi Technologies Ltd 
 * 	@package	Cloud3 Distribution !
 * 	@version	$REVISION$
 * 	@since		1.0 BETA 
 * 	@license	http://www.gnu.org/licenses/
 * 	@author		Faizal Pribadi 	<theblackshinobi@programmer.net>
 */

	class rss {
		private $output = null;
		private $protocol = null;
		private $cache_id = null;
		private $title = null;
		private $description = null;
		private $url = null;
		private $items = array();
		private $content_type = "application/rss+xml; charset=utf-8";

		/* Constructor
		 *
		 * INPUT:  object output
		 * OUTPUT: -
		 * ERROR:  -
		 */
		public function __construct($output) {
			$this->output = $output;

			if (isset($_SERVER["HTTP_SCHEME"])) {
				$this->protocol = $_SERVER["HTTP_SCHEME"];
			} else if ($_SERVER["HTTPS"] == "on") {
				$this->protocol = "https";
			} else {
				$this->protocol = "http";
			}

			$this->url = sprintf("%s://%s/", $this->protocol, $_SERVER["SERVER_NAME"]);
		}

		/* Magic method set
		 *
		 * INPUT:  string key
		 * OUTPUT: mixed value
		 * ERROR:  null
		 */
		public function __set($key, $value) {
			switch ($key) {
				case "title": $this->title = $value; break;
				case "description": $this->description = $value; break;
				case "url": $this->url = $value; break;
			}
		}

		/* Fetch RSS data from cache
		 *
		 * INPUT:  string cache id
		 * OUTPUT: -
		 * ERROR:  -
		 */
		public function fetch_from_cache($cache_id) {
			$this->output->content_type = $this->content_type;
			$this->cache_id = $cache_id;

			return $this->output->fetch_from_cache($cache_id);
		}

		/* Add RSS item
		 *
		 * INPUT:  string title, string description, string link, int timestamp
		 * OUTPUT: -
		 * ERROR:  -
		 */
		public function add_item($title, $description, $link, $timestamp) {
			if ($link[0] == "/") {
				$link = sprintf("%s://%s%s", $this->protocol, $_SERVER["SERVER_NAME"], $link);
			}

			array_push($this->items, array(
				"title"       => $title,
				"description" => $description,
				"link"        => $link,
				"timestamp"   => date("r", $timestamp)));
		}

		/* Send RSS feed to client
		 *
		 * INPUT:  -
		 * OUTPUT: -
		 * ERROR:  -
		 */
		public function to_output() {
			$this->output->content_type = $this->content_type;

			if ($this->cache_id !== null) {
				$this->output->start_caching($this->cache_id);
			}

			$this->output->open_tag("rss_feed");

			if ($this->title !== null) {
				$this->output->add_tag("title", $this->title);
			}
			if ($this->description !== null) {
				$this->output->add_tag("description", $this->description);
			}
			$this->output->add_tag("url", $url);

			$this->output->open_tag("items");
			foreach ($this->items as $item) {
				$this->output->record($item, "item");
			}
			$this->output->close_tag();

			$this->output->close_tag();

			if ($this->cache_id !== null) {
				$this->output->stop_caching();
			}
		}
	}
?>
